package edu.cs.usask.ca.silver.businesslogic.commands.generic;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.UUID;

import edu.cs.usask.ca.silver.api.businesslogic.ScenarioCollectionI;
import edu.cs.usask.ca.silver.api.persistence.StorageI;
import edu.cs.usask.ca.silver.businesslogic.ids.FileID;
import edu.cs.usask.ca.silver.businesslogic.ids.ModelVersionID;
import edu.cs.usask.ca.silver.businesslogic.ids.ScenarioCollectionID;
import edu.cs.usask.ca.silver.businesslogic.ids.ScenarioID;
import edu.cs.usask.ca.silver.businesslogic.util.SilverAutoManagedObjectFactory;

public class ScenarioCollection implements ScenarioCollectionI {

	private static final SilverAutoManagedObjectFactory scFactory = new SilverAutoManagedObjectFactory(
			"edu.cs.usask.ca.silver.businesslogic.commands.generic.ScenarioCollection");

	public static SilverAutoManagedObjectFactory getObjectPoolFactory() {
		return scFactory;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.cs.usask.ca.silver.api.businesslogic.ScenarioCollectionI#
	 * createScenarioCollection(java.lang.String, java.lang.String,
	 * java.lang.String,
	 * edu.cs.usask.ca.silver.businesslogic.ids.ModelVersionID)
	 */
	public ScenarioCollectionID createScenarioCollection(String scName,
			String scCreator, String scDescr, ModelVersionID mvId, StorageI storage)
			throws Exception {
		UUID temp = storage.addScenarioCollection(scName,
				scCreator, scDescr, mvId.getUUIdVal());
		return new ScenarioCollectionID(temp);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.cs.usask.ca.silver.api.businesslogic.ScenarioCollectionI#
	 * getScenarioCollectionAsXML
	 * (edu.cs.usask.ca.silver.businesslogic.ids.ScenarioCollectionID)
	 */
	public String getScenarioCollectionAsXML(ScenarioCollectionID scId, StorageI storage)
			throws Exception {
		String result = storage.getScenarioCollectionAsXML(
				scId.getUUIdVal());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cs.usask.ca.silver.api.businesslogic.ScenarioCollectionI#getName(
	 * edu.cs.usask.ca.silver.businesslogic.ids.ScenarioCollectionID)
	 */
	public String getName(ScenarioCollectionID scId, StorageI storage) throws Exception {

		String result = storage.getScenarioCollectionName(
				scId.getUUIdVal());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cs.usask.ca.silver.api.businesslogic.ScenarioCollectionI#getDescription
	 * (edu.cs.usask.ca.silver.businesslogic.ids.ScenarioCollectionID)
	 */
	public String getDescription(ScenarioCollectionID scId, StorageI storage) throws Exception {

		String result = storage
				.getScenarioCollectionDescription(scId.getUUIdVal());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cs.usask.ca.silver.api.businesslogic.ScenarioCollectionI#getCreator
	 * (edu.cs.usask.ca.silver.businesslogic.ids.ScenarioCollectionID)
	 */
	public String getCreator(ScenarioCollectionID scId, StorageI storage) throws Exception {

		String result = storage
				.getScenarioCollectionCreator(scId.getUUIdVal());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cs.usask.ca.silver.api.businesslogic.ScenarioCollectionI#getCreationDate
	 * (edu.cs.usask.ca.silver.businesslogic.ids.ScenarioCollectionID)
	 */
	public Date getCreationDate(ScenarioCollectionID scId, StorageI storage) throws Exception {

		Date result = storage
				.getScenarioCollectionCreationDate(scId.getUUIdVal());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cs.usask.ca.silver.api.businesslogic.ScenarioCollectionI#getCreationTime
	 * (edu.cs.usask.ca.silver.businesslogic.ids.ScenarioCollectionID)
	 */
	public Date getCreationTime(ScenarioCollectionID scId, StorageI storage) throws Exception {

		Date result = storage
				.getScenarioCollectionCreationTime(scId.getUUIdVal());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.cs.usask.ca.silver.api.businesslogic.ScenarioCollectionI#
	 * getParentVersionId
	 * (edu.cs.usask.ca.silver.businesslogic.ids.ScenarioCollectionID)
	 */
	public ModelVersionID getParentModelVersionId(ScenarioCollectionID scId, StorageI storage)
			throws Exception {
		UUID mvID = storage
				.getScenarioCollectionParentVersionId(scId.getUUIdVal());
		return new ModelVersionID(mvID);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cs.usask.ca.silver.api.businesslogic.ScenarioCollectionI#getScenarioIds
	 * (edu.cs.usask.ca.silver.businesslogic.ids.ScenarioCollectionID)
	 */
	public ScenarioID[] getScenarioIds(ScenarioCollectionID scId, StorageI storage)
			throws Exception {
		ArrayList<UUID> result;

		result = storage.getScenarioCollectionScenarioIds(
				scId.getUUIdVal());
		ScenarioID[] ids = new ScenarioID[result.size()];

		int i = 0;
		Iterator<UUID> e = result.iterator();
		while (e.hasNext()) {
			ids[i++] = new ScenarioID(e.next());
		}

		return ids;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.cs.usask.ca.silver.api.businesslogic.ScenarioCollectionI#
	 * getAssociatedFileIds
	 * (edu.cs.usask.ca.silver.businesslogic.ids.ScenarioCollectionID)
	 */
	public FileID[] getAssociatedFileIds(ScenarioCollectionID scId, StorageI storage)
			throws Exception {
		ArrayList<UUID> result;

		result = storage
				.getScenarioCollectionAssociatedFileIds(scId.getUUIdVal());
		FileID[] ids = new FileID[result.size()];

		int i = 0;
		Iterator<UUID> e = result.iterator();
		while (e.hasNext()) {
			ids[i++] = new FileID(e.next());
		}

		return ids;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.cs.usask.ca.silver.api.businesslogic.ScenarioCollectionI#
	 * getAssociatedFileName
	 * (edu.cs.usask.ca.silver.businesslogic.ids.ScenarioCollectionID)
	 */
	public ArrayList<String> getAssociatedFileName(ScenarioCollectionID scId, StorageI storage)
			throws Exception {

		return storage
				.getScenarioCollectionAssociatedFileNames(scId.getUUIdVal());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cs.usask.ca.silver.api.businesslogic.ScenarioCollectionI#getScenarios
	 * (edu.cs.usask.ca.silver.businesslogic.ids.ScenarioCollectionID)
	 */
	public HashMap<ScenarioID, String> getScenarios(ScenarioCollectionID scId, StorageI storage)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.cs.usask.ca.silver.api.businesslogic.ScenarioCollectionI#
	 * getMostRecentScenarioId
	 * (edu.cs.usask.ca.silver.businesslogic.ids.ScenarioCollectionID)
	 */
	public ScenarioID getMostRecentScenarioId(ScenarioCollectionID scId, StorageI storage)
			throws Exception {
		UUID sID = storage
				.getScenarioCollectionMostRecentScenarioId(scId.getUUIdVal());
		return new ScenarioID(sID);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cs.usask.ca.silver.api.businesslogic.ScenarioCollectionI#setName(
	 * edu.cs.usask.ca.silver.businesslogic.ids.ScenarioCollectionID,
	 * java.lang.String)
	 */
	public boolean setName(ScenarioCollectionID scId, String name, StorageI storage)
			throws Exception {
		return storage.setScenarioCollectionName(
				scId.getUUIdVal(), name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.cs.usask.ca.silver.api.businesslogic.ScenarioCollectionI#setDescr
	 * (edu.cs.usask.ca.silver.businesslogic.ids.ScenarioCollectionID,
	 * java.lang.String)
	 */
	public boolean setDescr(ScenarioCollectionID scId, String description, StorageI storage)
			throws Exception {
		return storage.setScenarioCollectionDescr(
				scId.getUUIdVal(), description);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.cs.usask.ca.silver.api.businesslogic.ScenarioCollectionI#
	 * deleteScenarioCollection
	 * (edu.cs.usask.ca.silver.businesslogic.ids.ScenarioCollectionID)
	 */
	public boolean deleteScenarioCollection(ScenarioCollectionID scId, StorageI storage)
			throws Exception {
		return storage.deleteScenarioCollection(
				scId.getUUIdVal());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.cs.usask.ca.silver.api.businesslogic.ScenarioCollectionI#
	 * duplicateScenario(edu.cs.usask.ca.silver.businesslogic.ids.ScenarioID,
	 * edu.cs.usask.ca.silver.businesslogic.ids.ScenarioCollectionID) TODO not
	 * yet implemented
	 */
	public ScenarioID duplicateScenario(ScenarioID sId,
			ScenarioCollectionID scId, StorageI storage) throws Exception {
		return new ScenarioID(storage.duplicateScenario(sId.getUUIdVal(), scId.getUUIdVal()));
	}

}
